/*
@title: CATCH ME!
@description: Catch Me if you can! is a pattern recognition game where you must navigate through increasingly complex levels to catch the goal. Each level presents a unique challenge requiring strategic movement to succeed. The game features multiple levels to test and enhance your pattern-solving skills.
@author: Nidheesh
@tags: []
@addedOn: 2023-01-07
*/

const player = "p"
const goal = "g"
const c = "c"
const secondc = "2"
const a = "a"
const t = "t"
const h = "h"
const m = "m"
const e = "e"
const explanationpoint = "!"
const o = "o"
const n = "n"
const g = "1"
const r = "r"
const u = "u"
const l = "l"
const i = "i"
const s = "s"
const melody = tune`
202.7027027027027,
202.7027027027027: f4-202.7027027027027,
202.7027027027027,
202.7027027027027: a4~202.7027027027027,
202.7027027027027: e5-202.7027027027027,
202.7027027027027: e5/202.7027027027027,
202.7027027027027,
202.7027027027027: c5~202.7027027027027,
202.7027027027027,
202.7027027027027: a5-202.7027027027027,
202.7027027027027: f4~202.7027027027027,
202.7027027027027: e5~202.7027027027027 + c4-202.7027027027027,
405.4054054054054,
202.7027027027027: g4^202.7027027027027 + e5/202.7027027027027,
405.4054054054054,
202.7027027027027: a4~202.7027027027027 + f5-202.7027027027027,
202.7027027027027: c4^202.7027027027027,
202.7027027027027,
202.7027027027027: b5^202.7027027027027,
405.4054054054054,
202.7027027027027: e5-202.7027027027027 + d4/202.7027027027027,
202.7027027027027,
202.7027027027027: e4~202.7027027027027,
202.7027027027027: b4^202.7027027027027,
202.7027027027027: b5/202.7027027027027,
202.7027027027027,
202.7027027027027: e5-202.7027027027027,
202.7027027027027: g5~202.7027027027027,
202.7027027027027`
const barrier = "b"

setLegend(
  [ player, bitmap`
.....000000.....
....02222220....
....02220200....
....02220200....
....02022220....
....02000020....
....02222220....
.....000000.....
.......0........
.....0000.......
....0..0.00.....
.......0........
.......0........
......0.0.......
......0..0......
......CC.CC.....`],
  [ goal, bitmap`
................
.33333333333333.
.30000300030003.
.30333303333033.
.30300300333033.
.30330303333033.
.30000300033033.
.33333333333333.
.30333030003033.
.30030030333033.
.30303030333033.
.30333030033033.
.30333030333333.
.30333030003033.
.33333333333333.
................`],
  [ c, bitmap`
................
.00000000000000.
.0............0.
.0............0.
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0............0.
.0............0.
.00000000000000.
................`],
  [ secondc, bitmap`
................
.00000000000000.
.0............0.
.0............0.
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0............0.
.0............0.
.00000000000000.
................`],
  [ a, bitmap`
................
.00000000000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
................`],
  [ t, bitmap`
................
.00000000000000.
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
................`],
  [ h, bitmap`
................
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
................`],
  [ m, bitmap`
................
.00...........0.
.0.00.......000.
.0...00...00..0.
.0.....0.0....0.
.0......0.....0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
................`],
  [ e, bitmap`

................
.00000000000000.
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.00000000.......
.0..............
.0..............
.0..............
.0..............
.0..............
.00000000000000.
................`],
  [ explanationpoint, bitmap`
................
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
................
........0.......
................`],
  [ o, bitmap`
................
.00000000000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
................`],
  [ n, bitmap`
................
.0............0.
.00...........0.
.0.0..........0.
.0..0.........0.
.0...0........0.
.0....0.......0.
.0.....0......0.
.0......0.....0.
.0.......0....0.
.0........0...0.
.0.........0..0.
.0..........0.0.
.0...........00.
.0............0.
................`],
  [ g, bitmap`
................
.00000000000000.
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0......0000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
................`],
  [ r, bitmap`
................
.00000000000000.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
.0.00...........
.0..00..........
.0...00.........
.0....00........
.0.....00.......
.0......00......
................`],
  [ u, bitmap`
................
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.0............0.
.00000000000000.
................`],
  [ l, bitmap`
................
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.0..............
.00000000000000.
................`],
  [ i, bitmap`
................
.00000000000000.
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
........0.......
.00000000000000.
................`],
  [ barrier, bitmap`
................
..333333333333..
.3...........33.
.3..........333.
.3.........33.3.
.3........33..3.
.3.......33...3.
.3......33....3.
.3.....33.....3.
.3....33......3.
.3...33.......3.
.3..33........3.
.3.33.........3.
.333..........3.
..333333333333..
................`]
);

setSolids([player, barrier]);

let level = 0
const levels = [
  map`
...........
...........
...p.......
...........
.cat2h.me!.
...........
.......g...
...........
...........`,
  map`
..........
..........
..........
pc........
..a.......
...t....g.
....c.....
.....h.me!
..........`,
  map`
..........
..........
..p.......
....h..!..
...c..e...
..t..m..g.
.a........
2.........`,
  map`
..........
..........
2.a.t.c.h.
p.........
..........
.....m.e.!
..........
........g.`,
  map`
2...................g.
.a....................
..t...................
...c..................
....h.................
......................
......................
......................
......................
......................
.....p................
......................
......................
......................
......................
...................m..
....................e.
.....................!`,
  map`
.......................
.......................
.......................
.......................
.......................
.......................
.......................
2at2h.me!!!............
.2at2h.me!!!.g.........
..2at2h.me!!!..........
...2at2h.me!!!.........
....2atch.me!!!........
.......................
.......................
.p.....................
.......................
.......................
.......................
.......................
.......................
.......................`,
  map`
...................
...................
..p................
...................
...................
...................
...a......m........
..2.t.h....e.......
.....c......!......
..b......b......g..
...................
...................
...................
...................
...................
...................
...................
...................`,
  map`
b.......p...............b.
..........................
..........................
..........................
..........................
...2atch............me!...
..........................
..........................
................b.......g.
..........................
..........................
..........................
..........................
..........................
..........................
..........................
b.......b.................
..........................
..........................
..........................`,
  map`
.............
.p........b..
.............
.............
.............
.2atch..me!..
.............
.............
.............
.............
.b........g..
.............`,
  map`
..b...b.bbbbb.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b.b.b.b..
..bbbbb.bbbbb.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b........
..b...b.b...b.b.b.b..
.....................
p2at2h.me!b.2at2h!..g
.....................
..b...b.bbbbb.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b.b.b.b..
..bbbbb.bbbbb.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b.b.b.b..
..b...b.b...b........
..b...b.b...b.b.b.b..`,
  map`
p..........b..........b
.......................
.......................
.......................
.......................
.......................
..2atch........m.e.!...
.......................
.......................
.......................
.......................
b..........b..........b
.......................
.......................
.......................
.......................
.......................
.......................
.......................
.......................
.......................
.......................
b..........b..........g`,
  map`
bbbbb
2at2b
bpbhb
bbgbb
me!bb`,
  map`
bbbbbbbbbg
bbbbbbbbbb
bb2atchbbb
bbbbbmepbb
bbbbbbbbbb
bbbbbbbbbb`,
  map`
2bbbbbbbba
hbbbbbbbbp
bbbbbbbbbb
bbbbbbbbbb
ebbbgbbbbm
2bbbbbbbbt`,
  map`
gatchbbmepb
bbbbbbbbbbb
bbbbbbbbbbb
bbbbbbbbbbb
bbbbbbbbbbb
bbbbbbbbbbb`,
  map`
pbbbbbbbbb
bbbbbbbbbb
2atchbme!b
bbbbbbbbbb
bbbbbbbbbb
bbbbbbbbbg`,
  map`
...............
.p.............
...............
...............
...............
...............
...............
2on1ratulation!
...............
...............
...............
...............
........g......
...............
...............`,
];

setMap(levels[level]);

playTune(melody, Infinity)

setPushables({
  [ player ]: [],
});

onInput("w", () => {
  getFirst(player).y -= level+1
  getFirst(goal).x -= level+1
});
onInput("a", () => {
  getFirst(player).x -= level+1
  getFirst(goal).y -= level+1
});
onInput("s", () => {
  getFirst(player).y += level+1
  getFirst(goal).x += level+1
});
onInput("d", () => {
  getFirst(player).x += level+1
  getFirst(goal).y += level+1
});

afterInput(() => {
   const PlayerOnGoal = tilesWith(goal, player)
   
  if (PlayerOnGoal.length > 0 && level<17) {
    level = level + 1
    addText("Level: "+level)
    if (level >= 11) {
      onInput("w", () => {
        getFirst(player).y -= 1
        getFirst(goal).x -= 1
      });
      onInput("a", () => {
        getFirst(player).x -= 1
        getFirst(goal).y -= 1
      });
      onInput("s", () => {
        getFirst(player).y += 1
        getFirst(goal).x += 1
      });
      onInput("d", () => {
        getFirst(player).x += 1
        getFirst(goal).y += 1
      });
    }
    if (level >= 16) {
        clearText()
    }
    if (level >= 17) {
        level = 0
    }
  setMap(levels[level]);  
}});